// 
// Soapi JavaScript Library RC2 Version 0.9 Revision 2010.06.29.001
// http://soapi.info/
// 
// Copyright 2010, Sky Sanders
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://soapi.info/code/js/license.aspx
// 
// Date: Thu July 01 2010
//     

/// <reference path="jquery-1.4.1.js" />
/// <reference path="qunit.js" />
/// <reference path="Soapi.js" />

var $s = Soapi;
var $sr = $s.Routes;
var $sd = $s.Domain;
var $sp = $s.Responses;


// caching interferes with this test suite
Soapi._disableCache = true;
// no need to queue- we are not hitting the api
$s.RequestQueue.setInterval(1);

function trimJsonp(url)
{
    //http:///1.0/users/1?jsonp=Soapi._internal._callback0
    //http:///1.0/users/1?
    return url.substring(0, url.indexOf("jsonp=Soapi._internal"));

}
module("vectorizedIdList");
test("", function()
{
    var list = new $s.Utilities.VectorizedIdList(200);
    for (var i = 0; i < 200; i++)
    {
        list.add(i);
    };
    var batches = list.getBatches();
    var total = 0;
    for (var i = 0; i < batches.length; i++)
    {
        total += batches[i].length;
    }
    ok(total == 200, "item count");

    batches = list.getBatchesVectorized();
    var passes = true;
    for (var i = 0; i < batches.length; i++)
    {
        if (batches[i].toString().length > 200)
        {
            passes = false;
            break;
        };
    }

    ok(passes, "vlist length");
});
module("applyIds");

// {Number|String},...
asyncTest("(Number)", function()
{
    expect(2);

    var r = $s.RouteFactory("", "").UsersById(1);
    ok(r.id[0] == 1);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/users/1?", lastUrl);
        start();
    };
    r.getResponse();

});


asyncTest("(Number,Number)", function()
{
    expect(4);

    var r = $s.RouteFactory().UsersById(1, 2);
    ok(r.id[0] == 1);
    ok(r.id[1] == 2);
    ok(r.id.length == 2);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/users/1;2?");
        start();
    };
    r.getResponse();

});

asyncTest("(String)", function()
{
    expect(2);

    var r = $s.RouteFactory().UsersById("foo");
    ok(r.id[0] == "foo");
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/users/foo?");
        start();
    };
    r.getResponse();

});

// {Number|String},..., Map
asyncTest("Number,Map", function()
{
    expect(3);

    var r = $s.RouteFactory().UsersById(1, { page: 1 });
    ok(r.id[0] == 1);
    ok(r.page == 1);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/users/1?page=1&");
        start();
    };
    r.getResponse();

});

asyncTest("Number,Number,Map", function()
{
    expect(5);

    var r = $s.RouteFactory().UsersById(1, 2, { page: 1 });
    ok(r.id[0] == 1);
    ok(r.id[1] == 2);
    ok(r.id.length == 2);
    ok(r.page == 1);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/users/1;2?page=1&");
        start();
    };
    r.getResponse();

});


// Array[Number|String]
asyncTest("Array[Number]", function()
{
    expect(4);

    var r = $s.RouteFactory().UsersById([1, 2]);
    ok(r.id[0] == 1);
    ok(r.id[1] == 2);
    ok(r.id.length == 2);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/users/1;2?");
        start();
    };
    r.getResponse();

});

// Array[Number|String], Map
asyncTest("Array[Number],Map", function()
{
    expect(5);

    var r = $s.RouteFactory().UsersById([1, 2], { page: 1 });
    ok(r.id[0] == 1);
    ok(r.id[1] == 2);
    ok(r.id.length == 2);
    ok(r.page == 1);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/users/1;2?page=1&");
        start();
    };
    r.getResponse();

});

// Map
asyncTest("Map", function()
{
    expect(1);

    var r = $s.RouteFactory().Search({ intitle: 1 });
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        ok(lastUrl == "http:///1.0/search?intitle=1&");
        start();
    };
    r.getResponse();

});


module("applyParameters");
test("ensure object copy", function()
{
    expect(3);
    var param = { date: new Date(), string: "FOO" };
    var obj = new $sd.Answer(param);
    ok(obj.date instanceof Date, "Date");
    ok(typeof (obj.string) == "string", "not string");
    ok(obj.string == "FOO", "FOO");
});




module("getResponse");
asyncTest("validateParameters search OK", function()
{
    expect(1);
    var params =
    {
        intitle: "intitle",
        nottagged: "nottagged",
        tagged: "tagged",
        sort: "activity",
        order: "desc",
        // min/max are special 'variant' params. you must explicitly set as Date object
        max: new Date(2005, 0, 19, 1, 30, 00),
        min: new Date(2005, 0, 19, 1, 00, 00),
        page: 2,
        pagesize: 3
    };

    var r = $s.RouteFactory().Search(params);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
        // intitle=intitle
        // nottagged=nottagged
        // tagged=tagged
        // sort=activity
        // order=desc
        // max=1106123400
        // min=1106121600
        // page=2
        // pagesize=3

        var expected = "http:///1.0/search?intitle=intitle&nottagged=nottagged&tagged=tagged&sort=activity&order=desc&max=1106123400&min=1106121600&page=2&pagesize=3&";
        ok(lastUrl == expected, "expected " + expected + " and got " + lastUrl);
        start();
    };
    r.getResponse();

});

asyncTest("validateParameters search NOT-OK", function()
{
    expect(1);

    var params = {};
    var r = $s.RouteFactory().Search(params);
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});
    };
    try
    {
        r.getResponse();
        ok(false, "expected error");
        start();
    }
    catch (error)
    {
        ok(error.message == "One of intitle, tagged, or nottagged must be set.");
        start();
    }
});



asyncTest("validateParameters fromdate/todate conversion string-date", function()
{
    expect(3);
    var params =
    {
        // todate/fromdate can be specified as Date, or parsed from a string.
        fromdate: new Date(2005, 0, 19, 1, 00, 00).toString(),
        todate: new Date(2005, 0, 19, 1, 30, 00)
    };

    var r = $s.RouteFactory().Tags(params);
    ok(r.todate.toString() == new Date(2005, 0, 19, 1, 30, 00).toString());
    ok(r.fromdate.toString() == new Date(2005, 0, 19, 1, 00, 00).toString());
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});

        var expected = "http:///1.0/tags?fromdate=1106121600&todate=1106123400&";
        ok(lastUrl == expected, "expected " + expected + " and got " + lastUrl);
        start();
    };
    r.getResponse();


});



asyncTest("validateParameters fromdate/todate conversion unix timestamp", function()
{
    expect(3);
    var params =
    {
        // todate/fromdate can be specified as Date, or parsed from a string or a unix timestamp (date.getTime()/1000).
        fromdate: new Date(2005, 0, 19, 1, 00, 00).getTime() / 1000,
        todate: new Date(2005, 0, 19, 1, 30, 00)
    };

    var r = $s.RouteFactory().Tags(params);
    ok(r.todate.toString() == new Date(2005, 0, 19, 1, 30, 00).toString());
    ok(r.fromdate.toString() == new Date(2005, 0, 19, 1, 00, 00).toString());
    var lastUrl;
    $s._internal.jsonpHandler = function(url, callback)
    {
        // get the url
        lastUrl = trimJsonp(url);
        // send back some json to kill the timeout
        callback({});

        var expected = "http:///1.0/tags?fromdate=1106121600&todate=1106123400&";
        ok(lastUrl == expected, "expected " + expected + " and got " + lastUrl);
        start();
    };
    r.getResponse();
});







